const app = getApp()
import trigger from '../../utils/util.js'
Page({
	data: {
		resourceDomain: app.resourceDomain,
		shadowShow: true,
		ruleShow: true,
		getPhoneBox:true
	},
	onLoad: function(options) {
    (trigger.trigger)(this)
		console.log(options)
		//授权
		app.checkSession(this,'../mall/goodsDetail',options.goods_id,options.type,options.id);
		// 判断手机号
		app.request(app.apiDomain + 'index.php?c=user&a=checkMobileBind', {}, res => {
			that.setData({
				phone_true:res.data.status
			})
		})
		// console.log(options)
		// console.log(options.id)
		var that = this;
		//公告
		app.request(app.apiDomain + "index.php?c=mall&a=notice", {
			type: 'lottery'
		}, function(res) {
			// console.log(res.data.data)
			that.setData({
				gao: res.data.data
			});
		});
		// 
		app.request(app.apiDomain + 'index.php?c=lottery&a=goods', {
			id: options.id
		}, function(res) {
			// console.log(res)
            // 如果未开奖 显示时间
            if(res.data.data.lottery_info.type=='0'){
                // 当前时间
                var today = new Date();
                var endDate = new Date(res.data.data.lottery_info.end_time.replace(/-/g,'/'));
                // 时间差
                var dayRange = endDate.getTime()-today.getTime()
                // 天数
                var days = Math.floor(dayRange/(24*3600*1000))
                //计算出小时数
                var leave=dayRange%(24*3600*1000) 
                var hours=Math.floor(leave/(3600*1000))
                console.log(111);
                that.setData({
                    days: days,
                    hours: hours
                })
            }
			// 参与按钮图片地址
			var TakePartUrl = ''
			// 参与文案
			var inviteContent = '邀请好友参加'
			// 背景
			var bgc = ''
			// 字体颜色
			var color = ''
			// 按钮状态
			var btnState = ''
			var my_status = res.data.data.lottery_info.my_status;
			if (res.data.status == 1) {
				if (my_status == 3) {
					// 已中奖已领取
					TakePartUrl = '//resource.160nurse.com/canvas/healthy/img/common/jian_6.png';
					inviteContent = '已领取'
					bgc = '#eee'
					color = '#666'
				} else if (my_status == 1) {
					// 未中奖未中奖
					TakePartUrl = '//resource.160nurse.com/canvas/healthy/img/common/jian_3.png';
					inviteContent = '未中奖'
					bgc = '#eee'
					color = '#666'
				} else if (res.data.data.lottery_info.type == 1 && my_status != 2) { // 未参与   是否结束
					// 提示已结束
					TakePartUrl = '//resource.160nurse.com/canvas/healthy/img/common/jian_4.png';
					inviteContent = '抽奖已结束';
					bgc = '#eee'
					color = '#666'
				} else if (my_status == -1) {
					// 未开奖 参与抽奖
					TakePartUrl = '//resource.160nurse.com/canvas/healthy/img/common/jian_1.png';
					inviteContent = '立即参与'
					btnState = '1'
				} else if (my_status == 0) {
					// 未开奖 已参与
					TakePartUrl = '//resource.160nurse.com/canvas/healthy/img/common/jian_2.png';
					inviteContent = '邀请好友参加'
					btnState = '2'
				} else if (my_status == 2) {
					// 已中奖已中奖
					TakePartUrl = '//resource.160nurse.com/canvas/healthy/img/common/jian_5.png';
					inviteContent = '立即领取'
					bgc = 'linear-gradient(to right,#F1228B,#FC673D);'
				}
			} else {
				console.log(res.data.info)
			}

			that.setData({
				goods: res.data.data.goods,
				lottery_info: res.data.data.lottery_info,
				lucky_list: res.data.data.lucky_list,
				prize_list: res.data.data.prize_list,
				range_width: (100 / parseInt(res.data.data.lottery_info.total)) * res.data.data.lottery_info.apply,
				TakePartUrl: TakePartUrl,
				inviteContent: inviteContent,
				bgc: bgc,
				color: color,
				btnState: btnState,
				goods_id: options.goods_id,
				id: options.id,
				user_id: options.user_id ? options.user_id : '',
				my_status: res.data.data.lottery_info.my_status,
				prize_id: res.data.data.lottery_info.prize_id,
			})
			var WxParse = require('../../wxParse/wxParse.js');
			var article = res.data.data.goods.f_detail_context;
			WxParse.wxParse('article', 'html', article, that, 0);
		})
	},
	gao_click: function() {
		this.setData({
			gao_on: 'on'
		})
	},
  getUserInfo: function () {
    var that = this
    if (app.getUserInfo(that)) {
      that.setData({
        user_auth: false
      })
    }
  },
	gao_off: function() {
		this.setData({
			gao_on: ''
		})
	},
	// 点击活动规则
	ruleView(e) {
		// console.log(111)
		this.setData({
			shadowShow: false,
			ruleShow: false
		})
	},
	// 活动规则,点击蒙层消失
	shadowClick(e) {
		// console.log(e)
		// 如果触发的规则是ruleInfo,则不消失
		if (e.target.dataset.name == 'ruleInfo') {
			return false
		}
		// console.log(this.data.ruleShow)
		// 如果规则展示
		if (!this.data.ruleShow) {
			this.setData({
				shadowShow: true,
				ruleShow: true
			})
		}
	},
	phone_click() {
		wx.makePhoneCall({
			phoneNumber: '4001000908'
		})
	},
	step_click() {
		wx.chooseAddress({
			success: (res) => {
				this.setData({
					stpe: res.provinceName + '-' + res.cityName + '-' + res.countyName + '-' + res.detailInfo
				});
				app.address = res;
			},
			fail: function(err) {
				// console.log(err)
			}
		})
	},
	// 参与按钮
	takePart(e) {
		console.log(111)
		var that = this
		// 如果是健康币兑换  提示请求
		if (this.data.lottery_info.payment_type == '1' && this.data.lottery_info.join == 0 && this.data.lottery_info.type ==
			0) {
			wx.showModal({
				title: '提示',
				content: '本次参与需消耗' + that.data.lottery_info.money + '个健康币',
				success(res) {
					if (res.confirm) {
						console.log('用户点击确定')
						app.request(app.apiDomain + 'index.php?c=lottery&a=detail', {
							lottery_id: that.data.id,
							user_id: that.data.user_id
						}, res => {
							console.log(res)

							if (res.data.status == 0) {
								wx.showToast({
									title: res.data.info,
									icon: 'none',
									duration: 2000
								})
							} else {
								var a = {};
								a.icon = wx.getStorageSync('icon');
								if (that.data.lucky_list.length < 6) {
									that.data.lucky_list.unshift(a)
								} else {
									that.data.lucky_list.unshift(a)
									that.data.lucky_list.splice(6, 1,'')
								}

								that.setData({
									TakePartUrl: '//resource.160nurse.com/canvas/healthy/img/common/jian_2.png',
									inviteContent: '邀请好友参加',
									btnState: '2',
									my_status: 0,
									['lottery_info.apply']: parseInt(that.data.lottery_info.apply) + 1,
									range_width: 100 / parseInt(that.data.lottery_info.total) * (parseInt(that.data.lottery_info.apply) +
										1),
									lucky_list: that.data.lucky_list
								})
							}
							// that.onLoad()
						})
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				}
			})
		} else if (this.data.lottery_info.payment_type == '2' && this.data.lottery_info.join == 0 && this.data.lottery_info
			.type == 0) {
			// 微信支付
			app.request(app.apiDomain + 'index.php?c=lottery&a=detail', {
				lottery_id: that.data.id,
				user_id: that.data.user_id
			}, res => {
				wx.requestPayment({
					'timeStamp': res.data.data.timeStamp,
					'nonceStr': res.data.data.nonceStr,
					'package': res.data.data.package,
					'signType': res.data.data.signType,
					'paySign': res.data.data.paySign,
					'success': function(res) {},
					'fail': function(res) {
						wx.showToast({
							title: '支付失败',
							icon: 'none',
							duration: 2000
						})
					},
					'complete': function(res) {
						console.log(res)
						if (res.errMsg == "requestPayment:ok") { // 调用支付成功
							var a = {};
							a.icon = wx.getStorageSync('icon');
							if (that.data.lucky_list.length < 6) {
								that.data.lucky_list.unshift(a)
							} else {
								that.data.lucky_list.unshift(a)
								that.data.lucky_list.splice(6, 1,'')
							}
							that.setData({
								TakePartUrl: '//resource.160nurse.com/canvas/healthy/img/common/jian_2.png',
								inviteContent: '邀请好友参加',
								btnState: '2',
								my_status: 0,
								['lottery_info.apply']: parseInt(that.data.lottery_info.apply) + 1,
								range_width: 100 / parseInt(that.data.lottery_info.total) * (parseInt(that.data.lottery_info.apply) +
									1),
								lucky_list: that.data.lucky_list
							})
						} else if (res.errMsg == 'requestPayment:cancel') {
							// 用户取消支付的操作
							wx.showToast({
								title: '已取消支付',
								icon: 'none',
								duration: 2000
							})
						}

					}
				})
			})
		}
	},
	onReady: function() {
		app.SystemInfo(this)
	},
	// 领取按钮
	mall_get: function() {
		// 判断手机号
		if(this.data.phone_true == 0) {
			// 未绑定手机  
			this.setData({
				getPhoneBox: false
			})
		} else {
			wx.navigateTo({
				url: '../Order/submit?goods_id=' + this.data.goods_id + '&prize_id=' + this.data.prize_id + '&id=' + this.data.id + '&sku=0&type=lottery'
			})
		}
	},
	getPhoneBox_click:function() {
		this.setData({
			getPhoneBox: true
		})
	},
	// 获取手机号信息  绑定
	getPhoneNumber(e) {
		console.log(e)
		app.request(app.apiDomain + 'index.php?c=user&a=userbinding', {
			encryptedData: e.detail.encryptedData,
			iv: e.detail.iv
		}, res => {
			console.log(res)
			if(res.data.status == 0){
				this.setData({
					getPhoneBox: true
				})
				return false
			}else{// 绑定成功
				wx.navigateTo({
					url: '../Order/submit?goods_id=' + this.data.goods_id + '&prize_id=' + this.data.prize_id + '&id=' + this.data.id + '&sku=0&type=lottery'
				})
			}
			
	
		})
	},
	onShareAppMessage: function() {
		// 判断是转发还是领奖
		var nickName = '';
		var userId = 0;
		nickName = wx.getStorageSync('nickname');
		userId = wx.getStorageSync('user_id');
		console.log(this.data.id)

		return {
			title: '免费抽取"'+ this.data.goods.f_name +'"快来看看!',
			path: '/pages/mall/goodsDetail?id=' + this.data.id + '&user_id' + userId
		}
	}
})
